package uns.fit.gis.ui;

import java.io.IOException;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

import uns.fit.gis.MapDrawer;
import uns.fit.gis.NokiaKeyDef;
import uns.fit.gis.i18n.LocaleManager;
import uns.fit.gis.mapdrawer.ActionCode;

public class GridMenuCanvas extends Canvas implements CommandListener {
	private MapDrawer midlet;
	private Command backCommand;
	private Command selectCommand;
	private Image menuImg;
	private Sprite pointer;
	private int menuId;
	private static final Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
	private static final String[] menuHelp = {
		LocaleManager.getMessage("showmap"),
		LocaleManager.getMessage("gps"),
		LocaleManager.getMessage("searchpaths"),
		LocaleManager.getMessage("searchlocations"),
		LocaleManager.getMessage("viewbestroute"),
		LocaleManager.getMessage("camera"),
		LocaleManager.getMessage("drivingassistance"),
		LocaleManager.getMessage("settings"),
		LocaleManager.getMessage("exit")
		};
	private static final int[] menuArray = {ActionCode.SHOW_MAP,
											ActionCode.RUN_GPS,
											ActionCode.SEARCH_PATH,
											ActionCode.SEARCH_LOCATION,
											ActionCode.SHOW_BEST_ROUTE,
											ActionCode.TAKE_PICTURE,
											ActionCode.ASSIST_DRIVING,
											ActionCode.SHOW_SETTINGS_FORM,
											ActionCode.DESTROY_APP
											};
	
	public GridMenuCanvas(MapDrawer midlet) {
		this.midlet = midlet;
		backCommand = new Command(LocaleManager.getMessage("back"), Command.BACK, 1);
		addCommand(backCommand);
		selectCommand = new Command(LocaleManager.getMessage("select"), Command.ITEM, 1);
		addCommand(selectCommand);
		setCommandListener(this);
		//setFullScreenMode(true);
		menuId = 0;
		
		try {
			menuImg = Image.createImage("/menu.png");
			pointer = new Sprite(Image.createImage("/pointer.png"));
		}
		catch(IOException ioe) {
			ioe.printStackTrace();
		}
	}
	
	protected void paint(Graphics g) {
		g.setColor(221, 238, 187);
		//g.setFont(font);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.drawImage(menuImg, 0, 0, Graphics.TOP | Graphics.LEFT);
		pointer.setPosition(menuId % 3 * 80, menuId / 3 * 80);
		pointer.paint(g);
		//System.out.println(menuHelp[menuId]);
		//g.setColor(0x000000);
		//g.drawString(menuHelp[menuId], 120, 260, Graphics.HCENTER | Graphics.BASELINE);
		this.setTitle(menuHelp[menuId]);
	}

	public void commandAction(Command command, Displayable displayable) {
		if(command == backCommand)
			midlet.showMapCanvas();
		else if(command == selectCommand)
			midlet.mapCanvas.commandAction(menuId);
	}

	protected void keyPressed(int keyCode)
	{
		//System.out.println(keyCode);
		//MapDrawer.showAlert(String.valueOf(keyCode));
		switch(keyCode) {
		case KEY_NUM0:
			//TODO: do what when press key 0?
			break;
		case KEY_NUM1:
			midlet.mapCanvas.commandAction(ActionCode.SHOW_MAP);
			break;
		case KEY_NUM2:
			midlet.mapCanvas.commandAction(ActionCode.RUN_GPS);
			break;
		case KEY_NUM3:
			midlet.mapCanvas.commandAction(ActionCode.SEARCH_PATH);
			break;
		case KEY_NUM4:
			midlet.mapCanvas.commandAction(ActionCode.SEARCH_LOCATION);
			break;
		case KEY_NUM5:
			midlet.mapCanvas.commandAction(ActionCode.SHOW_BEST_ROUTE);
			break;
		case KEY_NUM6:
			midlet.mapCanvas.commandAction(ActionCode.TAKE_PICTURE);
			break;
		case KEY_NUM7:
			midlet.mapCanvas.commandAction(ActionCode.ASSIST_DRIVING);
			break;
		case KEY_NUM8:
			midlet.mapCanvas.commandAction(ActionCode.SHOW_SETTINGS_FORM);
			break;
		case KEY_NUM9:
			midlet.mapCanvas.commandAction(ActionCode.DESTROY_APP);
			break;
		
		case RIGHT:
		case NokiaKeyDef.NOKIA_KEY_RIGHT_ARROW:
			if(menuId < 8) {
				menuId ++;
				repaint();
			}
			break;
		case LEFT:
		case NokiaKeyDef.NOKIA_KEY_LEFT_ARROW:
			if(menuId > 0) {
				menuId --;
				repaint();
			}
			break;
		case UP:
		case NokiaKeyDef.NOKIA_KEY_UP_ARROW:
			if((menuId - 3) >= 0) {
				menuId -= 3;
				repaint();
			}
			break;
		case DOWN:
		case NokiaKeyDef.NOKIA_KEY_DOWN_ARROW:
			if((menuId + 3) < 9) {
				menuId += 3;
				repaint();
			}
			break;
		case NokiaKeyDef.NOKIA_KEY_SELECT:
			midlet.mapCanvas.commandAction(menuArray[menuId]);
			break;
		default:
			break;
		}
	}
}
